package com.freak.kotlinfitnessserver.room.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.freak.kotlinfitnessserver.room.DeviceInfoData
import com.freak.kotlinfitnessserver.room.dao.DeviceInfoDataDao
import com.freak.kotlinfitnessserver.room.FeaturesData
import com.freak.kotlinfitnessserver.room.dao.FeaturesDataDao
import com.freak.kotlinfitnessserver.room.SupportRangeData
import com.freak.kotlinfitnessserver.room.dao.SupportRangeDataDao

// @Database 注解需要包含几个参数，Room 才能构建数据库。
//将 DeviceInfoData 指定为包含 entities 列表的唯一类。
//将 version 设为 1。每当您更改数据库表的架构时，都必须提升版本号。
//将 exportSchema 设为 false，这样就不会保留架构版本记录的备份。
// RoomDatabase是创建数据库，如果需要多个数据库，才需要多个RoomDatabase，否则只需要在entities里面添加数据表即可
@Database(
    entities = [DeviceInfoData::class, FeaturesData::class, SupportRangeData::class],
    version = 1,
    exportSchema = false
)
abstract class FitnessDeviceRoomDatabase : RoomDatabase() {
    abstract fun deviceInfoDataDao(): DeviceInfoDataDao
    abstract fun supportRangeDataDao(): SupportRangeDataDao
    abstract fun featuresDataDao(): FeaturesDataDao

    companion object {
        @Volatile
        private var INSTANCE: FitnessDeviceRoomDatabase? = null
        fun getDataBase(context: Context): FitnessDeviceRoomDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    FitnessDeviceRoomDatabase::class.java,
                    "fitness_device_database"
                )
                    .fallbackToDestructiveMigration()
                    .build()
                INSTANCE = instance
                return instance
            }
        }
    }
}